package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.MessageCenterAdapter;
import com.niji.littly_donkey_android.bean.Message;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.TimeUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import com.niji.littly_donkey_android.view.MessageCenterView;

import java.util.*;

public class MessageCenterActivity extends BaseActivity {

    private Button message_center_return;
    
    private Button message_center_follow;
    
    private Button message_center_fans;
    
    private Button message_center_comment;
    
    private Button message_center_notice;

    private MessageCenterView message_center_list;
    private List<Message> messageList;
    private MessageCenterAdapter messageAdapter;
    


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_centor_activity);
        init();
        messageCenterReturn();
        messageCenterFollow();
        messageCenterFans();
        messageCenterComment();
        messageCenterNotice();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.message_centor_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }

    private void init() {
        showMessageList();
    }


    /**
     * 展示通知列表
     */
    private void showMessageList() {
        message_center_list = findViewById(R.id.message_center_list);
        message_center_list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_inset));
        message_center_list.addItemDecoration(itemDecoration);
        messageList = new ArrayList<>();

        String url = UrlUtil.URL + "/user/comments/log/list?page=1&per_page=50";
        RequestQueue requestQueue= Volley.newRequestQueue(MessageCenterActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map  = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0){
                        Map<String, Object>  dataMap = (Map<String, Object>) map.get("data");
                        List<Map<String,Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        for (int i = 0; i < mapList.size(); i++) {
                            Integer id = (Integer) mapList.get(i).get("id");
                            Integer user_id = (Integer) mapList.get(i).get("user_id");
                            Integer comments_id = (Integer) mapList.get(i).get("comments_id");
                            Integer type = (Integer) mapList.get(i).get("type");
                            Integer status = (Integer) mapList.get(i).get("status");
                            String created_at = (String) mapList.get(i).get("created_at");
                            Map<String,Object> userInfo = (Map<String, Object>) mapList.get(i).get("userInfo");
                            String nickname = (String) userInfo.get("nickname");
                            String avatar = (String) userInfo.get("avatar");
                            if(status == 0){
                                Message inventory = new Message();
                                inventory.setTitle(nickname);
                                inventory.setTime(created_at);
                                inventory.setId(id);
                                inventory.setType(type);
                                inventory.setHead(avatar);
                                if (type == 1){
                                    inventory.setMessage(nickname + "点赞了你");
                                }else if(type == 2){
                                    inventory.setMessage(nickname + "评论了你");
                                }else if(type == 3){
                                    inventory.setMessage(nickname + "点赞了你的视频");
                                }
                                messageList.add(inventory);
                            }
                        }
                        messageAdapter = new MessageCenterAdapter(MessageCenterActivity.this, messageList);
                        message_center_list.setAdapter(messageAdapter);
                        messageAdapter.setOnDeleteClickListener(new MessageCenterAdapter.OnDeleteClickLister() {
                            @Override
                            public void onDeleteClick(View view, int position) {
                                Integer id = messageList.get(position).getId();
                                Integer type = messageList.get(position).getType();
                                alRead(id,type);
                                messageList.remove(position);
                                messageAdapter.notifyDataSetChanged();
                                message_center_list.closeMenu();
                            }
                        });
                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(MessageCenterActivity.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("错误", volleyError.toString());
                Toast.makeText(MessageCenterActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
            }
        }){


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(MessageCenterActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);

    }

    /**
     * 已读
     * @param id
     * @param type
     */
    private void alRead(Integer id, Integer type) {
        updateComments(id);

    }

    /**
     * 标记视频点赞消息为已读
     * @param id
     */
    private void updateLikes(Integer id) {
        String url = UrlUtil.URL + "/user/give/likes/update";
        RequestQueue requestQueue= Volley.newRequestQueue(MessageCenterActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map  = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0){
                        Toast.makeText(MessageCenterActivity.this,"标记已读成功",Toast.LENGTH_SHORT).show();

                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(MessageCenterActivity.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("错误", volleyError.toString());
                Toast.makeText(MessageCenterActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
            }
        }){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
                map.put("likes_id",String.valueOf(id));
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(MessageCenterActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);

    }

    /**
     * 表扣评论点赞消息为已读
     * @param id
     */
    private void updateComments(Integer id) {
        String url = UrlUtil.URL + "/user/comments/log/update";
        RequestQueue requestQueue= Volley.newRequestQueue(MessageCenterActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map  = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0){
                        Toast.makeText(MessageCenterActivity.this,"标记已读成功",Toast.LENGTH_SHORT).show();

                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(MessageCenterActivity.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("错误", volleyError.toString());
                Toast.makeText(MessageCenterActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
            }
        }){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
                map.put("comments_log_id",String.valueOf(id));
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(MessageCenterActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);
    }

    /**
     * 通知
     */
    private void messageCenterNotice() {
        message_center_notice = findViewById(R.id.message_center_notice);
        message_center_notice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MessageCenterActivity.this, NoticeActivity.class));
                   
            }
        });
    }


    /**
     * 评论
     */
    private void messageCenterComment() {
        message_center_comment = findViewById(R.id.message_center_comment);
        message_center_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MessageCenterActivity.this, FaulousActivity.class));
                   
            }
        });
    }

    /**
     * 粉丝
     */
    private void messageCenterFans() {
        message_center_fans = findViewById(R.id.message_center_fans);
        message_center_fans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MessageCenterActivity.this, FansActivity.class));
                   
            }
        });
    }

    /**
     * 关注
     */
    private void messageCenterFollow() {
        message_center_follow = findViewById(R.id.message_center_follow);
        message_center_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MessageCenterActivity.this, FollowActivity.class));
                   
            }
        });
    }


    /**
     * 返回
     */
    private void messageCenterReturn() {
        message_center_return = findViewById(R.id.message_center_return);
        message_center_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }
}
